package com.beeasy.hzbpm.entity;

import com.github.llyb120.json.Arr;
import com.github.llyb120.json.Obj;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.github.llyb120.json.Json.*;

public class BpmData {

    public Map<String,Node> nodes;

    //需要補充
    //构造成任务后需要补充的字段
    public Ext ext;

    // 线
//    public  Map<String,Line> lines;

    //列表字段
    public List<String> listFields;

    //起始节点ID，只能有一个，多个的情况下禁止保存
//    public String start;

    //结束节点ID，只能有一个，多个的情况下禁止保存
//    public String end;

    public String workflowName;

    // 其他设置
    public Obj elseSetting;
//    public Trigger trigger;


    public static class Node{
//        public Permission permission;

        public Config config;
        // 人员配置
        public Dealer dealers;

        // 表单字段
        public Fields fields;

        //流转设置
        public Flow flow;

        // 智能选人
//        public Intelligence intelligence;

        // 节点名
        public String id;
        public String name;

        //节点跳转的方向
        public List<NextNode> nextNodes = new ArrayList<>();

//        public List<String> quanxian;

        public Smart smart;

        public String type;

        public Timeout timeout;

        public Datasource datasource = new Datasource();
        public ChildProcess children;

        public boolean isEnd(){
            return id.startsWith("EndEvent");
        }

        public boolean upload(){
            try{
                return config.file.equals("allow") || config.file.equals("force");
            } catch (Exception e){return false; }
        }

        public boolean forceUpload(){
            try{
                return config.file.equals("force");
            } catch (Exception e){return false; }
        }


        /**
         * 无主办人会签
         * @return
         */
        public boolean noneSponsor(){
            try{
                return flow.sponsor.equals("none");
            } catch (Exception e){
                return false;
            }
        }

        /**
         * 先接收者为主办人
         */
        public boolean recipientSponsor(){
            try{
                return flow.sponsor.equals("recipient");
            } catch (Exception e){
                return false;
            }
        }

        /**
         * 允许会签
         * @return
         */
        public boolean allowCounterSign(){
            try{
                return flow.countersign.equals("allow");
            } catch (Exception e){return false; }
        }

        /**
         * 禁止会签
         * @return
         */
        public boolean disallowCounterSign(){
            try {
                return flow.countersign.equals("disallow");
            } catch (Exception e) {
                return true;
            }
        }

        /**
         * 允许并发
         * @return
         */
        public boolean concurrent(){
            try{
                return !flow.concurrent.equals("disallow");
            } catch (Exception e){
                return false;
            }
        }

        /**
         * 允许动态并发
         * @return
         */
        public boolean allowDynamic(){
            try{
                return flow.allowDynamic.equals("1");
            } catch (Exception e){
                return false;
            }
        }


        /**
         * 只允许选择本部门经办人
         * @return
         */
        public boolean filterSelfDep(){
            try{
                return smart.filterRule.equals("self_dep");
            } catch (Exception e){
                return false;
            }
        }

        public boolean filterParallelDep(){
            try{
                return smart.filterRule.equals("parallel_dep");
            } catch (Exception e){
                return false;
            }
        }


        /**
         * 是否使用数据源
         * @return
         */
        public boolean enableDataSource(){
            try{
                return datasource.enable.equals("allow");
            }catch (Exception e){
                return false;
            }
        }

        /**
         * 是否强制数据源
         * @return
         */
        public boolean forceDataSource(){
            try{
                return datasource.enable.equals("force");
            }catch (Exception e){
                return false;
            }
        }


        public String getDataSourceSql(){
            if(datasource == null){
                return "";
            }
            if(datasource.sql == null){
                return "";
            }
            return datasource.sql;
        }

        public Arr getAllowedChildNames(){
            try{
                return aaa(children.allows.entrySet().stream().map(e -> o("id", e.getKey(), "name", e.getValue().name)).collect(Collectors.toList()));
            } catch (Exception e){
                return a();
            }
        }


        /**
         * 是否手动开启子流程
         * @return
         */
        public boolean manualChildProcesses(){
            try {
                return children.manual.equals("1");
            } catch (Exception e){return false;}
        }

        /**
         * 是否强制开启子流程
         * @return
         */
        public boolean forceChildProcesses(){
            try {
                return children.force.equals("1");
            } catch (Exception e){return false;}
        }

        /**
         * 是否需要完成子流程
         * @return
         */
        public boolean hasFinishedChildProcesses(){
            try{
                return children.finished.equals("1");
            } catch (Exception e) {
                return false;
            }
        }
    }


    public static class Ext{
        public Map<String,Map> fields;
        public String xml;
        public String template;
    }


    /**
     * 在什么情况下可以跳转的节点
     */
    public static class NextNode{
        //节点的跳转条件配置
        //条件表达式
        public String expression;

        //需要跳转的节点ID
        public String node;
    }

    public static class Flow{
        public String  sponsor;
        public String  countersign;
        public String  concurrent;
        public String  changeAgent;
        public String  canSeeSign;
        public String  msgTemp;
        public String  merge;
        public String allowDynamic;
    }

    public static class Smart{
        public String filterRule;
        public String chooseRule;
        public String targetRule;
        public List<String> formFields;
    }

    public static class Dealer{
        public Map<String,String> users;
        public Map<String,String> deps;
        public Map<String,String> roles;
    }

    public static class Config{
        public List<String> permission;
        public String file;
    }

    public static class Timeout{
        public int minValue;
        public int stepValue;
        public int maxValue;
        public String minUnit;
        public String stepUnit;
        public String maxUnit;
    }

    public static class Datasource{
        public String enable = "none";
        public String sql = "";
    }

//    public static class Line{
//        public List<Condition> conditions;
//        public String expression;
//    }

    public static class Condition{
        public String field;
        public String op;
        public String value;
    }

    public static class ChildProcess{
        public Map<String, ChildProcessItem> allows;
        public String manual;
        public String force;
        public String finished;
    }
    public static class ChildProcessItem{
        public String name;
        public List<ChildProcessP2c> p2c;
        public List<ChildProcessP2c> c2p;
    }
    public static class ChildProcessP2c{
        public String from;
        public String to;
    }

//    public static class Permission{
//        public List<String> dids;
//        public List<String> rids;
//        public List<String> uids;
//        public Obj users;
//        public Obj roles;
//        public Obj deps;

//        public boolean allowUpload = false;
//        public boolean allowDownload = false;
//        public boolean allowBack = false;
//        public boolean departmentFirst = false;

//    }


    public static class Fields{
        //节点对应的表单属性
        public List<String> allFields;

        //必填的字段
        public List<String> requiredFields;
    }
    public static class Intelligence{
        public String chooseRule;
        public String filterRule;
        public String targetRule;
    }


}


